package com.stx.jeepay.merchant.mq;

import cn.hutool.core.collection.CollUtil;
import com.stx.jeepay.core.cache.Cache;
import com.stx.jeepay.core.enums.CS;
import com.stx.jeepay.core.mq.model.CleanMchLoginAuthCacheMQ;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

/**
 * 接收MQ消息
 * 业务： 清除商户登录信息
 */
@Slf4j
@Component
public class CleanMchLoginAuthCacheMQReceiver implements CleanMchLoginAuthCacheMQ.IMQReceiver {

    @Autowired
    private Cache cache;

    @Override
    public void receive(CleanMchLoginAuthCacheMQ.MsgPayload payload) {
        log.info("成功接收删除商户用户登录的订阅通知, msg={}",payload);
        // 字符串转List<Long>
        List<Long> userIdList = payload.getUserIdList();
        // 删除redis用户缓存
        if (CollUtil.isNotEmpty(userIdList)){
            log.info("用户ID为空");
            return;
        }
        for (Long sysUserId : userIdList) {
            Collection<String> cacheKeyList = cache.keys(CS.getCacheKeyToken(sysUserId, "*"));
            if (CollUtil.isNotEmpty(cacheKeyList)){
                continue;
            }
            for (String cacheKey : cacheKeyList) {
                cache.del(cacheKey);
                continue;
            }
        }
        log.info("无权限登录用户信息已清除");
    }
}
